package org.mindrive.system.core;

import java.io.Serializable;

import com.alibaba.fastjson.JSON;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 统一API响应结果封装
 */
@ApiModel(value = "接口通用返回结果结构", description = "接口通用返回结果结构")
public class Result implements Serializable {

	private static final long serialVersionUID = 1L;
	@ApiModelProperty(value = "编码", name = "code", dataType = "int", example = "200")
	private Integer code;
	@ApiModelProperty(value = "提示", name = "message", dataType = "string", example = "成功")
	private String message = "成功";
	@ApiModelProperty(value = "内容", name = "data", dataType = "t")
	private Object data;

	public Result() {

	}

	public Result success(String message) {
		this.message = message;
		this.code = ResultCode.SUCCESS.code();
		return this;
	}

	public static Result ok() {
		Result r = new Result();
		r.setCode(ResultCode.SUCCESS.code());
		r.setMessage("成功");
		return r;
	}

	public static Result ok(Object data) {
		Result r = new Result();
		r.setMessage("成功");
		r.setCode(ResultCode.SUCCESS.code());
		r.setData(data);
		return r;
	}

	public static Result error(String msg) {
		return error(ResultCode.FAIL.code(), msg);
	}

	public static Result confirm(String msg) {
		return error(ResultCode.CONFIRM.code(), msg);
	}

	public static Result error(int code, String msg) {
		Result r = new Result();
		r.setCode(code);
		r.setMessage(msg);
		return r;
	}

	public Result(int code, String message) {
		this.code = code;
		this.message = message;
	}

	public static Result noauth(String msg) {
		return error(ResultCode.PERMISSION_ERROR.code(), msg);
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	@Override
	public String toString() {
		return "Result{" +
				"code=" + code +
				", message='" + message + '\'' +
				", data=" + data +
				'}';
	}
}
